package com.mdd.admin.service.impl.yylq;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.yylq.ITAppBannerService;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.validate.yylq.TAppBannerCreateValidate;
import com.mdd.admin.validate.yylq.TAppBannerSearchValidate;
import com.mdd.admin.validate.yylq.TAppBannerUpdateValidate;
import com.mdd.admin.vo.yylq.TAppBannerDetailVo;
import com.mdd.admin.vo.yylq.TAppBannerListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppBanner;
import com.mdd.common.mapper.yangyangLeadingEnterprise.TAppBannerMapper;
import com.mdd.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 轮播图实现类
 * @author LikeAdmin
 */
@Service
public class TAppBannerServiceImpl implements ITAppBannerService {
        
    @Resource
    TAppBannerMapper tAppBannerMapper;

    /**
     * 轮播图列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<TAppBannerListedVo>
     */
    @Override
    public PageResult<TAppBannerListedVo> list(PageValidate pageValidate, TAppBannerSearchValidate searchValidate) {
        Integer page  = pageValidate.getPage_no();
        Integer limit = pageValidate.getPage_size();

        QueryWrapper<TAppBanner> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc(Arrays.asList("sort", "id"));

        tAppBannerMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:picUrl@pic_url:str",
            "=:sort:int",
            "=:type:int",
            "=:showType@show_type:int",
        });
        Integer showType = searchValidate.getShowType();
        queryWrapper.eq("show_type", showType);

        IPage<TAppBanner> iPage = tAppBannerMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<TAppBannerListedVo> list = new LinkedList<>();
        for(TAppBanner item : iPage.getRecords()) {
            TAppBannerListedVo vo = new TAppBannerListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.formatDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.formatDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 轮播图详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return TAppBanner
     */
    @Override
    public TAppBannerDetailVo detail(Integer id) {
        TAppBanner model = tAppBannerMapper.selectOne(
                new QueryWrapper<TAppBanner>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        TAppBannerDetailVo vo = new TAppBannerDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 轮播图新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(TAppBannerCreateValidate createValidate) {
        TAppBanner model = new TAppBanner();
        model.setPicUrl(createValidate.getPicUrl());
        model.setSort(createValidate.getSort());
        model.setType(createValidate.getType());
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        model.setShowType(createValidate.getShowType());
        tAppBannerMapper.insert(model);
    }

    /**
     * 轮播图编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(TAppBannerUpdateValidate updateValidate) {
        TAppBanner model = tAppBannerMapper.selectOne(
                new QueryWrapper<TAppBanner>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setPicUrl(updateValidate.getPicUrl());
        model.setSort(updateValidate.getSort());
        model.setType(updateValidate.getType());
        model.setUpdateTime(new Date());
        model.setShowType(updateValidate.getShowType());
        tAppBannerMapper.updateById(model);
    }

    /**
     * 轮播图删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        TAppBanner model = tAppBannerMapper.selectOne(
                new QueryWrapper<TAppBanner>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        tAppBannerMapper.delete(new QueryWrapper<TAppBanner>().eq("id", id));
    }

}
